<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="search-card">
      <el-form
        :model="queryParams"
        ref="queryParams"
        size="small"
        label-width="88px"
        class="formBox"
      >
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="订单号" prop="orderNumber">
              <el-input
                v-model="queryParams.orderNumber"
                placeholder="请输入订单号"
                maxlength="50"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="姓名" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入姓名"
                maxlength="50"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="操作类型" prop="operationType">
              <el-select
                v-model="queryParams.operationType"
                placeholder="请选择操作类型"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.operation_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select
                v-model="queryParams.paymentMethod"
                placeholder="请选择支付方式"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.payment_method"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="queryParams.status"
                placeholder="请选择状态"
                style="width: 100%"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.order_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="支付类型" prop="onlineType">
              <el-select
                v-model="queryParams.onlineType"
                placeholder="请选择支付类型"
                clearable
                style="width: 100%"
              >
                <el-option label="在线支付" value="1" />
                <el-option label="离线支付" value="0" />
                <el-option label="现金充值" value="2" />
                <el-option label="在线退款" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="支付时间" prop="time">
              <el-date-picker
                v-model="queryParams.time"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item>
              <div class="dis aic">
                <div class="flex_1"></div>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                >
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
                  重置
                </el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 按钮 -->
    <el-card class="search-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['order:flow:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>
    <!-- 列表 -->
    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="flowList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="订单号" align="center" prop="orderNumber" width="320" />
        <el-table-column label="金额" align="center" prop="operationAmount">
          <template slot-scope="scope">
            {{ scope.row.operationAmount ? scope.row.operationAmount : "0" }}元
          </template>
        </el-table-column>
        <el-table-column label="手续费" align="center" prop="handlingFee">
          <template slot-scope="scope">
            {{ scope.row.handlingFee ? scope.row.handlingFee : "0" }}元
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="name" />
        <el-table-column
          label="身份证号"
          align="center"
          prop="idCardNumber"
          width="180"
        />
        <el-table-column label="余额" align="center" prop="postOperationBalance">
          <template slot-scope="scope">
            {{ scope.row.postOperationBalance ? scope.row.postOperationBalance : "0" }}元
          </template>
        </el-table-column>
        <el-table-column label="操作类型" align="center" prop="operationType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.operation_type"
              :value="scope.row.operationType"
            />
          </template>
        </el-table-column>
        <el-table-column label="支付方式" align="center" prop="paymentMethod">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.payment_method"
              :value="scope.row.paymentMethod"
            />
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.order_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="支付类型" align="center" prop="onlineType">
          <template slot-scope="scope">
            <span v-if="scope.row.onlineType == '1'">在线支付</span>
            <span v-if="scope.row.onlineType == '0'">离线支付</span>
            <span v-if="scope.row.onlineType == '2'">现金充值</span>
            <span v-if="scope.row.onlineType == '3'">在线退款</span>
          </template>
        </el-table-column>
        <el-table-column label="支付时间" align="center" prop="operationTime" width="180">
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.operationTime, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              class="padding_4_10"
              @click="refundStatusFun(scope.row)"
              v-if="
                scope.row.status == '4' &&
                (scope.row.operationType == '2' ||scope.row.operationType == '5') &&
                scope.row.orderFinish != '1'
              "
              >因错退款
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改交易流水对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCardNumber">
          <el-input v-model="form.idCardNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="人员Id" prop="personId">
          <el-input v-model="form.personId" placeholder="请输入人员Id" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="平台订单号" prop="platformOrderNumber">
          <el-input v-model="form.platformOrderNumber" placeholder="请输入平台订单号" />
        </el-form-item>
        <el-form-item label="通道订单号" prop="channelOrderNumber">
          <el-input v-model="form.channelOrderNumber" placeholder="请输入通道订单号" />
        </el-form-item>
        <el-form-item label="操作金额" prop="operationAmount">
          <el-input v-model="form.operationAmount" placeholder="请输入操作金额" />
        </el-form-item>
        <el-form-item label="手续费" prop="handlingFee">
          <el-input v-model="form.handlingFee" placeholder="请输入手续费" />
        </el-form-item>
        <el-form-item label="操作后余额" prop="postOperationBalance">
          <el-input v-model="form.postOperationBalance" placeholder="请输入操作后余额" />
        </el-form-item>
        <el-form-item label="操作类型" prop="operationType">
          <el-select v-model="form.operationType" placeholder="请选择操作类型">
            <el-option
              v-for="dict in dict.type.operation_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-select v-model="form.paymentMethod" placeholder="请选择支付方式">
            <el-option
              v-for="dict in dict.type.payment_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作人" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="操作时间" prop="operationTime">
          <el-date-picker
            clearable
            v-model="form.operationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择操作时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker
            clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改消费记录对话框 -->
    <el-dialog title="应错退款" :visible.sync="tk_open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="退款金额" prop="refundAmount" class="is-required">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="退款理由" prop="refundReason">
          <el-input v-model="form.refundReason" placeholder="请输入退款理由" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="tk_submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlow, getFlow, delFlow, addFlow, updateFlow } from "@/api/order/flow";
import { consumptionflowRefund } from "@/api/order/consumption";

export default {
  name: "Flow",
  dicts: ["order_status", "operation_type", "payment_method"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 交易流水表格数据
      flowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      tk_open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        idCardNumber: null,
        orderNumber: null,
        platformOrderNumber: null,
        channelOrderNumber: null,
        operationType: null,
        paymentMethod: null,
        status: null,
        operator: null,
        createdAt: null,
        time: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  watch: {
    "queryParams.time": function (newVal) {
      console.log(newVal);
      if (!newVal) {
        this.queryParams.time=[];
        this.queryParams.paymentTimeStart = null;
        this.queryParams.paymentTimeEnd = null;
      }
    },
  },
  created() {
    this.getList();
  },
  activated() {
    this.$forceUpdate();
  },
  methods: {
    /** 查询交易流水列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.time.length == 2) {
        this.queryParams.paymentTimeStart = this.queryParams.time[0];
        this.queryParams.paymentTimeEnd = this.queryParams.time[1];
      }
      listFlow(this.queryParams).then((response) => {
        this.flowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.tk_open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        idCardNumber: null,
        personId: null,
        orderNumber: null,
        platformOrderNumber: null,
        channelOrderNumber: null,
        operationAmount: null,
        handlingFee: null,
        postOperationBalance: null,
        operationType: null,
        paymentMethod: null,
        status: null,
        operator: null,
        operationTime: null,
        createdAt: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.queryParams.time = [];
      this.queryParams.paymentTimeStart = null;
      this.queryParams.paymentTimeEnd = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加交易流水";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getFlow(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改交易流水";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateFlow(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlow(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除交易流水编号为"' + ids + '"的数据项？')
        .then(function () {
          return delFlow(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "order/flow/export",
        {
          ...this.queryParams,
        },
        "资金流水" + `_${new Date().getTime()}.xlsx`
      );
    },
    refundStatusFun(options) {
      this.form = options;
      this.tk_open = true;
    },
    /** 提交按钮 */
    tk_submitForm() {
      this.$refs["form"].validate((valid) => {
        if (this.form.refundAmount == 0) {
          return this.$modal.msgError("请输入正确的退款金额");
        }
        if (this.form.id != null) {
          consumptionflowRefund(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        } else {
          consumptionflowRefund(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
